package org.csu.mypetstore_seller.controller;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.csu.mypetstore_seller.domain.Order;
import org.csu.mypetstore_seller.domain.OrderStatus;
import org.csu.mypetstore_seller.persistence.OrderStatusMapper;
import org.csu.mypetstore_seller.service.OrderStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.text.ParseException;
import java.util.List;
@Controller
@RequestMapping("orderstatus")
@Slf4j

public class OrderStatusController {
    @Autowired
    HttpSession httpSession;
    @Autowired
    OrderStatusService orderStatusService;

    @GetMapping("orderStatus")
    public String orderStatus(Model model){
        log.info("访问订单状态管理页面");
        model.addAttribute("orderstatuses", orderStatusService.getAllOrderListStatus());
        return "order/orderstatus";
    }

    @GetMapping("deleteorderstatus")
    public String deleteOrderStatus(@RequestParam("id") String id, Model model) throws ParseException {
        log.info("删除订单状态记录，订单ID：{}", id);
        orderStatusService.deleteOrderStatusById(Integer.parseInt(id));
        return getString(model);
    }

    //根据会话保存的搜索条件显示列表
    public String getString(Model model) throws ParseException {
        if(httpSession.getAttribute("method")!=null){//如果之前有保存的搜索条件
            String method =httpSession.getAttribute("method").toString();
            String str=httpSession.getAttribute("str").toString();
            log.info("根据已保存的搜索条件查询订单状态，方式：{}，关键词：{}", method, str);
            //根据搜索条件获取订单
            List<OrderStatus> orderStatuses=orderStatusService.getOrderStatus(method,str);
            model.addAttribute("orderstatuses",orderStatuses);
        }else{
            log.info("显示所有订单状态记录");
            List<OrderStatus> orderStatuses=orderStatusService.getAllOrderListStatus();
            model.addAttribute("orderstatuses",orderStatuses);
        }
        return "order/orderstatus";

    }

    // 搜索订单状态由字段和关键词


    @GetMapping("searchorderstatus")
    public String searchOrderStatus(@RequestParam("method")String method,
                                  @RequestParam("str")String str,
                                  Model model) throws ParseException {
        if(str!=null){
            log.info("使用{}方式搜索订单状态，关键词：{}", method, str);
            httpSession.setAttribute("method",method);
            httpSession.setAttribute("str",str);
            try{
                List<OrderStatus> orderStatuses=orderStatusService.getOrderStatus(method,str);
                System.out.println(orderStatuses);
                if(orderStatuses.equals(null)||orderStatuses.get(0)==null) {
                    log.warn("未找到符合条件的订单状态记录");
                    throw new IllegalArgumentException("未找到订单");
                } else {
                    log.info("查询到{}条订单状态记录", orderStatuses.size());
                    model.addAttribute("orderstatuses",orderStatuses);
                    return "order/orderstatus";
                }
            }  catch (ParseException e) {
            log.error("日期格式解析失败: {}", str, e);
            throw new IllegalArgumentException("未找到订单");
        }

        }
        return "catalog/error";
    }

    //标记订单是发货的状态
    @GetMapping("sendoutstatus")
    public String sendOutStatus(@RequestParam("id")String id,Model model) throws ParseException {
        try {
            log.info("开始处理订单发货，订单ID：{}", id);
            orderStatusService.sendOutOrderById(Integer.parseInt(id));
            model.addAttribute("message", "发货成功");
            log.info("订单{}发货成功", id);
        } catch (RuntimeException e) {
            log.error("订单{}发货失败：{}", id, e.getMessage());
            model.addAttribute("message", e.getMessage());
        }
        return getString(model);
    }
}